
from django.db import models
from django.utils import timezone
class Category(models.Model):
    name = models.CharField(max_length=100, verbose_name='分类名称')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'app_category_auto'
        verbose_name = '问题分类'
        verbose_name_plural = '问题分类'

class QuestionAnswer(models.Model):
    sku = models.CharField(max_length=50, blank=True, null=True, verbose_name='SKU')
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='问题分类')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"SKU: {self.sku}"

    class Meta:
        db_table = 'app_questionanswer_auto'
        verbose_name = '问题答案'
        verbose_name_plural = '问题答案'

class QuestionAnswerTranslation(models.Model):
    question_answer = models.ForeignKey(QuestionAnswer, on_delete=models.CASCADE, related_name='translations')
    language = models.CharField(max_length=10, verbose_name='语言代码')
    question = models.TextField(verbose_name='问题')
    answer = models.TextField(verbose_name='答案')
    image_url = models.URLField(blank=True, null=True, verbose_name='图片链接')
    image_alt = models.CharField(max_length=200, blank=True, null=True, verbose_name='图片描述')

    class Meta:
        db_table = 'app_questionanswertranslation_auto'
        unique_together = ('question_answer', 'language')
        verbose_name = '问题答案翻译'
        verbose_name_plural = '问题答案翻译'

class Keyword(models.Model):
    word = models.CharField(max_length=100, verbose_name='关键词')
    synonyms = models.TextField(blank=True, null=True, verbose_name='同义词')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.word

    class Meta:
        db_table = 'app_keyword_auto'
        verbose_name = '关键词'
        verbose_name_plural = '关键词'

class UserRequest(models.Model):
    user_input = models.TextField(verbose_name='用户输入')
    language_code = models.CharField(max_length=10, verbose_name='语言代码')
    request_time = models.DateTimeField(default=timezone.now, verbose_name='请求时间')
    response = models.TextField(blank=True, null=True, verbose_name='系统响应')
    matched_questions = models.ManyToManyField(QuestionAnswer, blank=True, verbose_name='匹配的问题')

    def __str__(self):
        return f"{self.user_input} - {self.request_time}"

    class Meta:
        db_table = 'app_userrequest_auto'
        verbose_name = '用户请求'
        verbose_name_plural = '用户请求'